package com.example.jdbcdemo.service.Impl;

import com.example.jdbcdemo.mapper.CollectMoneyOrderMapper;
import com.example.jdbcdemo.mapper.CustomerMapper;
import com.example.jdbcdemo.mapper.GroupIdMapMapper;
import com.example.jdbcdemo.pojo.CollectMoneyOrder;
import com.example.jdbcdemo.pojo.Customer;
import com.example.jdbcdemo.service.StatsCustServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class StatsCustServiceImpl implements StatsCustServiceInterface {

    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CollectMoneyOrderMapper collectMoneyOrderMapper;
    @Autowired
    GroupIdMapMapper groupIdMapMapper;

    @Override
    public List<Map<String, Object>> StatsCustomerSales() {
        List<Customer> customerList= customerMapper.queryCustomerList();//返回客户列表
        List<Map<String,Object>> resultList=new LinkedList<>();
        for (Customer customer:customerList){
            int customerId=customer.getId();
            List<CollectMoneyOrder> collectMoneyOrderList=collectMoneyOrderMapper.queryCollectMoneyOrderByCustomerId(customerId);
            double totalPrice=0;
            double acceptPrice=0;
            for (CollectMoneyOrder collectMoneyOrder:collectMoneyOrderList){
                totalPrice+=collectMoneyOrder.getTotalPrice();
                acceptPrice+=collectMoneyOrder.getAcceptPrice();
            }
            Map<String,Object> tempMap=new LinkedHashMap<>();
            tempMap.put("customerId",customerId);
            tempMap.put("customerName",customer.getName());
            tempMap.put("totalPrice",totalPrice);
            tempMap.put("acceptPrice",acceptPrice);
            tempMap.put("debtPrice",totalPrice-acceptPrice);//欠款
            resultList.add(tempMap);
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> queryCollectOrderListByCustId(int customerId) {
        List<CollectMoneyOrder> orderList=collectMoneyOrderMapper.queryCollectMoneyOrderByCustomerId(customerId);
        List<Map<String, Object>> resultList=new LinkedList<>();
        for (CollectMoneyOrder collectMoneyOrder:orderList)
        {
            Map<String,Object> map=new LinkedHashMap<>();
            Customer customer=customerMapper.queryCustomerById(collectMoneyOrder.getCustomerId());
            map.put("groupId",collectMoneyOrder.getGroupId());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            map.put("date",sdf.format(groupIdMapMapper.queryGroupIdMapById(collectMoneyOrder.getGroupId()).getDate()));
            int type=customer.getCustomerType();
            String types="";
            switch (type){
                case 0:
                    types="零售";
                    break;
                case 1:
                    types="批发";
                    break;
                default:
                    break;
            }
            map.put("customerType",types);//0零售1批发
            map.put("totalPrice",collectMoneyOrder.getTotalPrice());
            map.put("acceptPrice",collectMoneyOrder.getAcceptPrice());
            int status=collectMoneyOrder.getStatus();
            String statuss="";
            switch (status){
                case 0:
                    statuss="未结清";
                    break;
                case 1:
                    statuss="已结清";
                    break;
                default:
                    break;
            }
            map.put("status",statuss);//0未结清1已结清
            map.put("statusCode",status);
            resultList.add(map);
        }
        return resultList;
    }
}
